#!/usr/bin/env python3

import sys
import json
import os.path
import argparse

import mail
import addrbook



parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()

account_parser = subparsers.add_parser("account", help="manage your mail accounts")

addrbook_parser = subparsers.add_parser("addrbook", aliases=["b"], help="manage your address book")
addrbook_parser.add_argument('name', action="store", default="__all__", help="list the mail addresses belonging to the name, default to show all")
addrbook_parser.set_defaults(func=addrbook.main)

check_parser = subparsers.add_parser("check")

retrieve_parser = subparsers.add_parser("retrieve", aliases=["r", ">"], help="retrieve mail from the remote server")

send_parser = subparsers.add_parser("send", aliases=["s", "<"], help="send a mail using one of the accounts")
send_parser.add_argument('-s', '--subject', action="store", type=str, dest='subject', default='-- No subject --')
send_parser.add_argument('-a', '--attach', action="store", type=str, dest='attach', default=None, metavar="ATTACHMENT")
send_parser.add_argument('-t', '--text', action="store", type=str, dest='plain', default='-- No text --')
send_parser.add_argument('-f', '--from', action="store", type=str, dest='source')
send_parser.add_argument('-2', '--dest', action="store", type=str, dest='destination')
send_parser.set_defaults(func=mail.main)

opts = parser.parse_args()
# if not opts:
#     parser.print_help()
# else:
# opts.func(opts, send_parser.print_help)
opts.func(opts, parser.print_help)
